const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];
var app = getApp()
//获取年
for (let i = 1900; i <= date.getFullYear() + 5; i++) {
  years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  months.push("" + i);
}
//获取日期
for (let i = 1; i <= 31; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  days.push("" + i);
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: '男',
        value: '男',
      },
      {
        name: '女',
        value: '女',
      }
    ],
    // 时间选择器
    time: '',
    multiArray: [years, months, days],
    multiIndex: [0, 9, 16, 10, 17],
    choose_year: '',
    // 下拉框
    selectShow: false, //控制省下拉列表的显示隐藏，false隐藏、true显示
    selectShows: false, //控制市下拉列表的显示隐藏，false隐藏、true显示
    selectData: [], //下拉列表的数据
    selectDatas: ['上海', '北京', '广州', '山西'],


    index: 0, //选择省的下拉列表下标 
    indexs: 0, //选择市的下拉列表下标 
    // 患者关系
    sickrela: [{
        relation: '自己',
        index: 0
      },
      {
        relation: '家庭成员',
        index: 1
      },
      {
        relation: '亲戚',
        index: 2,
      },
      {
        relation: '朋友',
        index: 3
      },
      {
        relation: '其他',
        index: 4
      }
    ],
    sick_index: 1,
    sickShows: false, //控制市下拉列表的显示隐藏
    // 证件
    papers: [{
        cert: '身份证'
      },
      {
        cert: '护照'
      },
      {
        cert: '军人证'
      },
      {
        cert: '台胞证'
      },
      {
        cert: '港澳居民内地同行证'
      },
      {
        cert: '宝宝卡'
      }
    ],
    pap_index: 0,
    paperShows: false,
    region: ["四川省", "广元市", "苍溪县"],
    sex: ""
  },
  radioChange: function (e) {
    console.log(e)
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var items = this.data.items;
    this.setData({
      sex: e.detail.value
    })
    var items = this.data.items;
    for (var i = 0; i < items.length; ++i) {
      items[i].checked = items[i].value == e.detail.value
    }
    console.log(items)
    this.setData({
      items: items
    });
  },
  // 患者关系
  choseRelation: function (e) {
    console.log(e)
    var that = this
    var ownsick_index = that.data.ownsick_index
    if (ownsick_index == 0) {
      wx.showToast({
        title: '不能更改自己的家庭关系',
        icon: 'none'
      })
      return false
    }
    this.setData({
      sick_index: e.currentTarget.dataset.index,
    })
  },
  // 证件类型
  certificate: function (e) {
    console.log(e)
    this.setData({
      pap_index: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */



  onLoad: function (options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    var j_id = options.j_id
    if (options.sick_index) {
      var sick_index = options.sick_index
      if (sick_index == 0) {
        console.log(that.data.sickrela, options.sick_index)
        that.setData({
          sickrela: that.data.sickrela,
          sick_index: parseInt(options.sick_index)
        })
      } else {
        if (options.sickrela) {
          var sickrela = JSON.parse(options.sickrela)
        } else {
          console.log('222')
          var sickrela = [{
              relation: '家庭成员',
              index: 1
            },
            {
              relation: '亲戚',
              index: 2,
            },
            {
              relation: '朋友',
              index: 3
            },
            {
              relation: '其他',
              index: 4
            }
          ]
        }
        that.setData({
          sickrela: sickrela,
          sick_index: parseInt(options.sick_index)
        })
      }
    }
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    //设置默认的年份
    that.setData({
      choose_year: this.data.multiArray[0][0],
      bgc: bgc,
      j_id: j_id
    })
    that.getmydan()
  },
  // 点击下拉显示框
  selectTap(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        selectShow: !this.data.selectShow
      });
    } else if (e.currentTarget.dataset.index == 1) {
      this.setData({
        selectShows: !this.data.selectShows
      });
    } else if (e.currentTarget.dataset.index == 2) {
      this.setData({
        sickShows: !this.data.sickShows
      });
    } else if (e.currentTarget.dataset.index == 3) {
      this.setData({
        paperShows: !this.data.paperShows
      });
    }
  },
  // 地区选择器
  changeRegin(e) {
    this.data.region = e.detail.value
    this.setData({
      region: this.data.region
    })
  },

  sicksTap(e) {
    let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    this.setData({
      sick_index: Index,
      sickShows: !this.data.sickShows
    });
  },
  paperTap(e) {
    let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    this.setData({
      pap_index: Index, //证件类型
      paperShows: !this.data.paperShows
    });
  },
  bindMultiPickerChange: function (e) {
    console.log(e)
    console.log('picker发送选择改变，携带下标为', e.detail.value)
    // console.log('picker发送选择改变，携带值为', this.data.array[index])
    this.setData({
      multiIndex: e.detail.value
    })
    const index = this.data.multiIndex;
    const year = this.data.multiArray[0][index[0]];
    const month = this.data.multiArray[1][index[1]];
    const day = this.data.multiArray[2][index[2]];
    // const hour = this.data.multiArray[3][index[3]];
    // const minute = this.data.multiArray[4][index[4]];
    this.setData({
      time: e.detail.value
    })
    // console.log(this.data.time);
  },
  //监听picker的滚动事件
  bindMultiPickerColumnChange: function (e) {
    //获取年份
    if (e.detail.column == 0) {
      let choose_year = this.data.multiArray[e.detail.column][e.detail.value];
      console.log(choose_year);
      this.setData({
        choose_year
      })
    }
    //console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    if (e.detail.column == 1) {
      let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
      let temp = [];
      if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //判断31天的月份
        for (let i = 1; i <= 31; i++) {
          if (i < 10) {
            i = "0" + i;
          }
          temp.push("" + i);
        }
        this.setData({
          ['multiArray[2]']: temp
        });
      } else if (num == 4 || num == 6 || num == 9 || num == 11) { //判断30天的月份
        for (let i = 1; i <= 30; i++) {
          if (i < 10) {
            i = "0" + i;
          }
          temp.push("" + i);
        }
        this.setData({
          ['multiArray[2]']: temp
        });
      } else if (num == 2) {
        let year = parseInt(this.data.choose_year);
        console.log(year);
        if (((year % 400 == 0) || (year % 100 != 0)) && (year % 4 == 0)) {
          for (let i = 1; i <= 29; i++) {
            if (i < 10) {
              i = "0" + i;
            }
            temp.push("" + i);
          }
          this.setData({
            ['multiArray[2]']: temp
          });
        } else {
          for (let i = 1; i <= 28; i++) {
            if (i < 10) {
              i = "0" + i;
            }
            temp.push("" + i);
          }
          this.setData({
            ['multiArray[2]']: temp
          });
        }
      }
      console.log(this.data.multiArray[2]);
    }
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  bindsubmit: function (e) {
    console.log(e)
    var that = this
    var pap_index = that.data.pap_index
    var sick_index = that.data.sick_index
    var region = that.data.region
    var valuearr = e.detail.value
    var datetime = valuearr.datetime
    var names = valuearr.names
    var numcard = valuearr.numcard
    var tel = valuearr.tel
    var sex = that.data.sex
    var openid = wx.getStorageSync('openid')
    var ownsick_index = that.data.ownsick_index
    console.log(sex, datetime, region, sick_index, tel, numcard)
    if (sex == 0) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return false
    }
    if (!datetime) {
      wx.showToast({
        title: '请选择出生日期',
        icon: 'none'
      })
      return false
    }
    if (region == 0) {
      wx.showToast({
        title: '请选择所在城市',
        icon: 'none'
      })
      return false
    }

    if (that.data.j_id) {
      app.util.request({
        'url': 'entry/wxapp/User.updateinfo',
        data: {
          pap_index: pap_index,
          sick_index: sick_index,
          region: region,
          datetime: datetime,
          names: names,
          numcard: numcard,
          tel: tel,
          sex: sex,
          openid: wx.getStorageSync('openid'),
          j_id: that.data.j_id
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '修改成功',
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  detail: 1
                })
              }, 2000)
            }
          })
        }
      })
      return false
    }
    app.util.request({
      'url': 'entry/wxapp/User.addjiaren',
      data: {
        pap_index: pap_index,
        sick_index: sick_index,
        region: region,
        datetime: datetime,
        names: names,
        numcard: numcard,
        tel: tel,
        sex: sex,
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '添加成功',
          success: function () {
            setTimeout(function () {
              wx.navigateBack({
                detail: 1
              })
            }, 2000)
          }
        })
      }
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  getmydan() {
    var that = this
    var j_id = that.data.j_id
    app.util.request({
      'url': 'entry/wxapp/User.detailmyjd',
      data: {
        j_id: j_id
      },
      success: function (res) {
        console.log(res)
        if (res.data) {
          var region = res.data.region
          var ss = region.split(",");
          var papers = that.data.papers
          var pap_index = res.data.pap_index
          var item = papers[pap_index]
          that.setData({
            myinfo: res.data,
            region: ss,
            time: res.data.datetime,
            item: item,
            sex: res.data.sex,
            ownsick_index: res.data.sick_index
          })
          var items = that.data.items;
          for (var i = 0; i < items.length; i++) {
            if (items[i].value == res.data.sex) {
              items[i].checked = true;
            }
          }
          that.setData({
            items: items
          })
        }
      }
    })
  },
  checkphone(e) {
    console.log(e)
    var that = this
    var phone = e.currentTarget.dataset.phone
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/tel/tel?u_phone='+phone,
    })
  }

})